package health.lm.com.mine.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.lm.sjy.titlebar.widget.CommonTitleBar;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.widget.EditTextWithDel;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.present.EditBankCardPresenter;
import health.lm.com.router.ARouterConstant;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_LEFT_BUTTON;

/**
 * 修改/添加银行卡
 */
@Route(path = ARouterConstant.EditBankCardActivity)
public class EditBankCardActivity extends XActivity<EditBankCardPresenter> {
    @BindView(R2.id.title_bar)
    CommonTitleBar titlebar;
    @BindView(R2.id.et_card_name)
    EditText etCardName;
    @BindView(R2.id.tv_bank_name)
    EditText etBankName;
    @BindView(R2.id.et_card_num)
    EditTextWithDel etCardNum;
    @BindView(R2.id.tv_submit)
    TextView tvSubmit;
    @BindView(R2.id.ll_layout)
    LinearLayout llLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_bank_card;
    }

    @Override
    public EditBankCardPresenter newP() {
        return new EditBankCardPresenter();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData(Bundle savedInstanceState) {
        //titlebar 返回按钮
        titlebar.setListener((v, action, extra) -> {
            if (action == ACTION_LEFT_BUTTON) {
                finish();
            }
        });
        //验证金额和银行卡
        Observable<CharSequence> nameObservale = RxTextView.textChanges(etCardName).debounce(500, TimeUnit.MILLISECONDS);
        Observable<CharSequence> cardObservale = RxTextView.textChanges(etCardNum).debounce(500, TimeUnit.MILLISECONDS);
        Observable<CharSequence> bankObservale = RxTextView.textChanges(etBankName).debounce(500, TimeUnit.MILLISECONDS);
        Observable.combineLatest(nameObservale, cardObservale,bankObservale, (name, card, bank) -> {
            boolean isCard = card.toString().length() > 0;
            boolean isMoney = name.toString().length() > 0;
            boolean isBank = bank.toString().length() > 0;

            return isCard && isMoney && isBank;
        }).compose(bindToLife())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aBoolean -> RxView.enabled(tvSubmit).accept(aBoolean));
        //提交
        RxView.clicks(tvSubmit)
                .throttleFirst(2, TimeUnit.SECONDS)
                .compose(bindToLife())
                .subscribe(o -> {
                    getP().addBank(etCardName.getText().toString(),etBankName.getText().toString() , etCardNum.getText().toString());
                });
    }

    public void addBankCardSuccess() {
        finish();
    }

    public void editBankCardSuccess() {
        finish();
    }

}
